'''json 方法使用'''
import json


#  Python 字典类型转换为 JSON 对象
data1 = {
    'no': 1,
    'name': 'Runoob',
    'url': 'http://www.runoob.com'
}


def object_to_jsonstring(data, num: int) -> str | None:
    """ dict对象转字符串/文件"""
    if (num is None or num == 0):
        json_str = json.dumps(data)
        print("JSON data=", json_str)
        return json_str

    with open("data.json", "w", encoding="UTF-8") as file:
        json.dump(data, file)
        print("dict 转文件")
        return None


def string_to_object(json_str):
    """字符串转dict对象"""
    obj = json.loads(json_str)
    print("raw data=", obj)
    return obj


def use_loads(file_path):
    """文件转dict"""
    with open(file_path, 'r', encoding="UTF-8") as file:
        data = json.load(file)
        return data


# object_to_jsonstring(data)

STR = '{"no": 1, "name": "Runoob", "url": "http://www.runoob.com"}'
dict_obj = string_to_object(STR)
